﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HelperPrototype._DB
{
    class PhieuDeNghiMuaHang_HangHoaDB
    {
        public static IQueryable selectAll(_DB.DataClasses1DataContext db)
        {
            if (db == null)
            {
                db = new _DB.DataClasses1DataContext();
            }
            var query = from u in db.PhieuDeNghiMuaHang_HangHoas
                    //    where u.deleted ==false
                        select u;
            return query;
        }
        public static PhieuDeNghiMuaHang_HangHoa selectID(String ma, _DB.DataClasses1DataContext db)
        {
            if (db==null)
            {
                 db = new _DB.DataClasses1DataContext();
            }
           
            var query = from u in db.PhieuDeNghiMuaHang_HangHoas
                        where u.MaHangHoa == ma
                        select u ;
            if (query.Count()>0)
            {
                return query.First();
            }
            return null;
            //return query;
        }
        public static PhieuDeNghiMuaHang_HangHoa selectID(String maHang,String maPhieu, _DB.DataClasses1DataContext db)
        {
            if (db == null)
            {
                db = new _DB.DataClasses1DataContext();
            }

            var query = from u in db.PhieuDeNghiMuaHang_HangHoas
                        where u.MaHangHoa == maHang && u.MaPhieuDeNghiMuaHang == maPhieu
                        select u;
            if (query.Count() > 0)
            {
                return query.First();
            }
            return null;
            //return query;
        }
        public static bool insert(_DB.PhieuDeNghiMuaHang_HangHoa u ,_DB.DataClasses1DataContext db)
        {
            if (db == null)
            {
                db = new _DB.DataClasses1DataContext();
            }
            db.PhieuDeNghiMuaHang_HangHoas.InsertOnSubmit(u);
            db.SubmitChanges();
            return true;
        }
        public static void update(PhieuDeNghiMuaHang_HangHoa newPhieu, _DB.DataClasses1DataContext db)
        {
             if (db == null)
            {
                db = new _DB.DataClasses1DataContext();
            }
            var query = (from u in db.PhieuDeNghiMuaHang_HangHoas
                        where u.MaHangHoa == newPhieu.MaHangHoa
                        select u);
        //    MessageBox.Show(query.Count().ToString());
            if (query.Count() > 0)
            {
                PhieuDeNghiMuaHang_HangHoa p = query.First();
                p.MaPhieuDeNghiMuaHang = newPhieu.MaPhieuDeNghiMuaHang;
                p.SoLuong = newPhieu.SoLuong;
                p.Gia = newPhieu.Gia;
                p.NhaCungCap = newPhieu.NhaCungCap;
               
             //   p.deleted = newPhieu.deleted;
                db.SubmitChanges();
              //  return query.First();
            }
            return ;
            //return query;
        }
        public static void deleteOnPhieu(String maPhieu, _DB.DataClasses1DataContext db)
        {
            if (db == null)
            {
                db = new _DB.DataClasses1DataContext();
            }
            var query = from u in db.PhieuDeNghiMuaHang_HangHoas
                        where u.MaPhieuDeNghiMuaHang == maPhieu
                        select u;
        //    MessageBox.Show(query.Count().ToString());
            foreach (PhieuDeNghiMuaHang_HangHoa h in query) 
            {
                db.PhieuDeNghiMuaHang_HangHoas.DeleteOnSubmit(h);
            }
            db.SubmitChanges();
            return;
            //return query;
        }
        /*
        public static void delete(String id,_DB.DataClasses1DataContext db)
        {
            if (db == null)
            {
                db = new _DB.DataClasses1DataContext();
            }
            var query = from u in db.PhieuDeNghiMuaHang_HangHoas
                        where u.MaHangHoa == id
                        select u;
            if (query.Count() > 0)
            {
                PhieuDeNghiMuaHang_HangHoa p = query.First();
                p.deleted = true;
                db.SubmitChanges();
            }
            return ;
            //return query;
        }
         */ 
    }
    
}
